package cn.engage.controller;

import cn.engage.domain.User;
import cn.engage.feign.UserFeignClient;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {
  @Autowired
  private UserFeignClient userFeignClient;

  @GetMapping("/{id}")
  @SentinelResource(value = "getUserById", blockHandler = "getUserByIdBlockHandler",
    fallback = "getUserByIdFallback")
  public User getUserById(@PathVariable("id") Long id){
   // int a = 1/0;
    return userFeignClient.getUserById(id);
  }


    public User getUserByIdBlockHandler(@PathVariable("id") Long id, BlockException blockException){
        return new User(-1L, "限流了", "超过最大流量！！！");
    }
    public User getUserByIdFallback(@PathVariable("id") Long id, Throwable throwable) {
          return new User(-1L, "熔断了", "要断了要断了！！！！");
      }
}
